<template>
  <div>
    <div class="mod-material">
      <!-- 查询及操作区域 -->
      <el-form
        class="content-top-form"
        id="content-top-form"
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <div class="actionBarLeft">
          <el-form-item>
            <el-input v-model="dataForm.name" placeholder="搜索名称" clearable style="width:100px;"></el-input>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="start"
              align="right"
              type="date"
              placeholder="请选择开始日期"
              :picker-options="pickerOptions"
              style="width:145px;"
            ></el-date-picker>
            <el-date-picker
              v-model="end"
              align="right"
              type="date"
              placeholder="请选择结束日期"
              :picker-options="pickerOptions"
              style="width:145px;"
            ></el-date-picker>
            <el-button @click="getDataList()">查询</el-button>
          </el-form-item>
        </div>
        <div class="actionBarRight">
          <el-form-item>
            <el-button
              v-if="isAuth('depot:material:save')"
              type="primary"
              @click="addOrUpdateHandle()"
            >新增
            </el-button>
            <el-button
              v-if="isAuth('depot:material:delete')"
              type="danger"
              @click="deleteHandle()"
              :disabled="dataListSelections.length <= 0"
            >批量删除
            </el-button>
          </el-form-item>
          <!-- 导出 -->
          <el-form-item>
            <el-tooltip class="item" effect="dark" content="可以根据指定的结构将数据导出到Excel表格中" placement="top">
              <el-button type="success" icon="el-icon-setting">导出</el-button>
            </el-tooltip>
          </el-form-item>
          <!-- 导入 -->
          <el-form-item>
            <el-tooltip class="item" effect="dark" content="可以根据指定的结构将数据一次性导入到系统" placement="top">
              <el-button type="success" icon="el-icon-setting">导入</el-button>
            </el-tooltip>
          </el-form-item>
          <!-- 打印表格 -->
          <el-form-item>
            <el-button type="success" @click="printSelect" icon="el-icon-setting">打印所选</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="printByNewPage" icon="el-icon-setting">新页打印</el-button>
          </el-form-item>

          <!-- 帮助 -->
          <el-form-item>
            <el-tooltip class="item" effect="dark" content="点击查看帮助" placement="top">
              <el-popover placement="bottom" title="标题" width="500" trigger="click">
                <div>
                  <div>本表为员工信息表，主要包含员工基本信息和员工工资信息。为了更加专注于员工工资信息，员工的详细信息默认被隐藏了。</div>
                  <div>如果需要查看员工详细信息，请点击每一行左侧的“>”进行查看。</div>
                  <div>注意：员工删除要慎重，特别是采用批量删除的方式删除员工是一件危险的事情，如果误删除信息，请联系管理员进行恢复操作。</div>
                </div>
                <el-button slot="reference" type="primary" icon="el-icon-question">帮助</el-button>
              </el-popover>
            </el-tooltip>
            <!-- end -->
          </el-form-item>
        </div>
      </el-form>

      <!-- 数据表 -->
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
        fixed
        @select="selectRow"
        @select-all="selectRow"
      >
        <!-- 选择员工 -->
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <!-- 主要显示内容 -->
        <el-table-column prop="name" header-align="center" align="center" label="物品名称"></el-table-column>
        <el-table-column prop="spec" header-align="center" align="center" label="规格"></el-table-column>
        <el-table-column prop="price" header-align="center" align="center" label="价格"></el-table-column>
        <el-table-column prop="unit" header-align="center" align="center" label="单位"></el-table-column>
        <el-table-column prop="amount" header-align="center" align="center" label="库存数量"></el-table-column>
        <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="isAuth('depot:material:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.materialId)"
            >修改
            </el-button>
            <el-button
              v-if="isAuth('depot:material:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.materialId)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <print v-if="printVisible" ref="print"></print>
  </div>
</template>

<script>
  import AddOrUpdate from './material-add-or-update'
  import print from '../../common/print'

  export default {
    data () {
      return {
        dataForm: {
          userName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        printVisible: false,
        selection: [],

        start: 0,
        end: 0,
        pickerOptions: {
          disabledDate (time) {
            return time.getTime() > Date.now()
          },
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        }
      }
    },
    components: {
      AddOrUpdate,
      print
    },
    mounted () {
    },
    watch: {},

    activated () {
      this.getDataList()
    },
    computed: {},
    methods: {
      printByNewPage () {
        this.$router.replace({name: 'example-material-print', params: {data: this.selection}})
      },

      /**
       * 打印选择的行，到另外一个组件中打印
       */
      selectRow (selection) {
        this.selection = selection
        console.log(this.selection)
      },

      printSelect () {
        console.log(this.selection)
        this.printVisible = true
        this.$nextTick(() => {
          this.$refs.print.init(this.selection)
        })
      },

      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.dataForm.name
        }
        this.service.examplePrintMaterialList(params).then(res => {
          if (res) {
            this.dataList = res.data.list
            this.totalPage = res.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/depot/material/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style lang="css" scoped>

  /* 顶部操作部分布局 */
  .content-top-form {
    display: flex;
    justify-content: space-between;
  }

  .actionBarLeft,
  .actionBarRight {
    display: flex;
  }

  /* 详情展开 */
  .table-expand-info {
    padding-left: 100px;
    font-size: 0;
  }

  .table-expand-info .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

  .table-expand-info .table-expand-label {
    color: #99a9bf;
    width: 90px;
  }

  /* 分页居中 */
  .site-wrapper .el-pagination {
    text-align: center;
  }

</style>
